/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import target.Word;

/**
 *将编辑的单词的内容写到XML文件里
 * @author GeH
 */
public class WordWriting {

    public WordWriting() {
    }

    public void wordWriting(Word word) {
        String wordname = word.word;

        try {

            SAXReader saxReader = new SAXReader(); 
            saxReader.setEncoding("UTF-8"); //按照UTF-8的编码方式读取
            Document document = saxReader.read(new File("dic/UserList.xml"));

            List list = document.selectNodes("/wordbook/item");
            Iterator iter = list.iterator();

            while (iter.hasNext()) {
                Element itemElement = (Element) iter.next();
                Iterator iterator = itemElement.elementIterator("word");
                while (iterator.hasNext()) {
                    Element wordElement = (Element) iterator.next();
                    //以单词类的单词属性为标识，查找并修改
                    if (wordElement.getText().equals(wordname)) {

                        iterator = itemElement.elementIterator("status");
                        while (iterator.hasNext()) {
                            Element statusElement = (Element) iterator.next();

                            if (word.status == word.status.DUMP) {
                                statusElement.setText("0");
                            }

                            if (word.status == word.status.REM) {
                                statusElement.setText("1");
                            }

                            if (word.status == word.status.NREM) {
                                statusElement.setText("2");
                            }
                        }

                        iterator = itemElement.elementIterator("rating");
                        while (iterator.hasNext()) {
                            Element ratingElement = (Element) iterator.next();
                            ratingElement.setText(String.valueOf(word.rating));
                        }

                        iterator = itemElement.elementIterator("content");
                        while (iterator.hasNext()) {
                            Element contentElement = (Element) iterator.next();
                            contentElement.setText(word.content);
                        }

                        iterator = itemElement.elementIterator("tricks");
                        while (iterator.hasNext()) {
                            Element tricksElement = (Element) iterator.next();
                            tricksElement.setText(word.tricks);
                        }

                        iterator = itemElement.elementIterator("date");
                        while (iterator.hasNext()) {
                            Element dateElement = (Element) iterator.next();
                            dateElement.setText(word.date);
                        }
                    }
                    break;
                }
            }
            try {
                OutputFormat format = new OutputFormat();
                format.setEncoding("UTF-8");//按照UTF-8的编码方式写入
                XMLWriter writer= new XMLWriter(new FileOutputStream("dic/UserList.xml"),format);
                writer.write(document);
                writer.close();

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
